<?php

	// TITLE: Municipality AUTO MODEL
	// FILE: scaffolding/model/municipality.php
	// AUTHOR: AUTOGEN
	// CREATED: 2015/04/20 07:49:57
	// DESCRIPTION: 
	// REQUIREMENTS: 

	$createMunicipality = function($con, $municipality) {
		$sql = 'INSERT INTO municipality (id, title, type_id) VALUES (';
		$sql .= $municipality->getId() . ', ';
		$sql .= $municipality->getTitle() . ', ';
		$sql .= $municipality->getTypeId() . ')';
		$sql .= ')';
		$result = mysqli_query($con, $sql);
		$id = ($result) ? mysqli_insert_id($con) : $result;
		mysqli_close($con);
		return $id;
	};

	$readMunicipality = function($con, $id) {
		$sql = 'SELECT * FROM municipality WHERE id = ' . $id;
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$result = mysqli_fetch_array($results);
			$municipality = new Municipality();
			$municipality->setId(result[0]);
			$municipality->setTitle(result[1]);
			$municipality->setTypeId($dbio->select($readType(result[2])));
		} else {
			$municipality = false;
		}
		return $municipality;
	};

	$updateMunicipality = function($con, $municipality) {
		$sql = 'UPDATE municipality SET ';
		$sql .= 'id = ' . $municipality->getId() . ', ';
		$sql .= 'title = ' . $municipality->getTitle() . ', ';
		$sql .= 'type_id = ' . $municipality->getTypeId() ;
		$sql .= 'WHERE id = ' . id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$deleteMunicipality = function($con, $id) {
		$sql = 'DELETE FROM municipality WHERE id = ' . $id;
		$result = mysqli_query($con, $sql);
		mysqli_close($con);
		return $result;
	};

	$listMunicipality = function($con) {
		$sql = 'SELECT * FROM municipality';
		$results = mysqli_query($con, $sql);
		mysqli_close($con);
		if ($results) {
			$municipalitys = array();
			while ($result = mysqli_fetch_array($results)) {
				$municipality = new Municipality();
				$municipality->setId(result[0]);
				$municipality->setTitle(result[1]);
				$municipality->setTypeId($dbio->select($readType(result[2])));
				$municipalitys[] = municipality;
			}// end while
		} else {
			$municipalitys = false;
		}
		return $municipalitys;
	};

